﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Web;
using Galaktika.BI.Silverlight.Services;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess
{
    public abstract class DbQueryExecutor : IQueryExecutor
    {
        protected DbQueryExecutor()
        {
        }

        protected abstract DbConnection CreateConnection(string connectionString);
        protected abstract DbDataAdapter CreateDataAdapter(string query, DbConnection conn);
        protected abstract DbCommand CreateCommand(string query, DbConnection conn);

        private DbCommand m_CurrentCmd;
        private DbDataAdapter m_CurrentAdapter;

        #region IQueryExecutor Members

        public InvokeResultSchema ExecuteQuery(string connectionString, string query, string sessionId)
        {
            InvokeResultSchema res = new InvokeResultSchema();
            res.ContentType = KnownContentTypes.RelationalData;
            using (var conn = this.CreateConnection(connectionString))
            {
                conn.Open();
                using (m_CurrentAdapter = this.CreateDataAdapter(query, conn))
                {
                    m_CurrentAdapter.SelectCommand.Connection = conn;
                    var dt = new DataTable();
                    m_CurrentAdapter.Fill(dt);
                    res.Content = XmlUtility.Obj2XmlStr(DataTableHelper.Create(dt));
                }
                lock (this)
                {
                    m_CurrentCmd = null;
                }
                conn.Close();
            }

            return res;
        }

        public InvokeResultSchema ExecuteNonQuery(string connectionString, string query, string sessionId)
        {
            InvokeResultSchema res = new InvokeResultSchema();
            res.ContentType = KnownContentTypes.RelationalData;
            using (var conn = this.CreateConnection(connectionString))
            {
                conn.Open();
                using (m_CurrentCmd = this.CreateCommand(query, conn))
                {
                    res.Content = m_CurrentCmd.ExecuteNonQuery().ToString();
                }
                lock (this)
                {
                    m_CurrentCmd = null;
                }
                conn.Close();
            }

            return res;
        }

        public bool Cancel()
        {
            lock (this)
            {
                if (m_CurrentCmd != null)
                {
                    m_CurrentCmd.Cancel();

                    return true;
                }
            }

            lock (this)
            {
                if (m_CurrentAdapter != null)
                {
                    m_CurrentAdapter.SelectCommand.Cancel();

                    return true;
                }
            }

            return false;
        }

        #endregion
    }
}
